<?php
declare (strict_types=1);

namespace app\common\logic\payment\admin;

use app\admin\model\market\VipConfig;
use app\common\model\payment\admin\PaymentOrderItem as POI;
use app\common\model\ProductType;
use app\course\model\base\Course;
use app\course\model\base\CourseSection;
use app\exam\model\admin\ModuleExamination;
use app\exam\model\admin\ModuleRollup;
use app\exam\model\admin\ModuleSpecial;
use app\mall\model\product\Product;

/**
 * 订单模型
 */
class PaymentOrderItem extends POI
{
    /**
     * 根据用户ID获取用户的购买记录
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-31
     *
     * @param int $user_id [description]
     * @return   [type]                                  [description]
     */
    public function getOrderListByUserId(int $user_id)
    {
        return $this
            ->withJoin(['pdorder' => ['status', 'payment_time']])
            ->whereIn('pdorder.status', [20, 30, 40])
            ->where('__TABLE__.user_id', $user_id)
            ->order('pdorder.payment_time', 'desc')
            ->append(['from_text'])
            ->paginate($this->getListRows())
            ->each(function ($item) {
                // 购买时间
                $payment_time       = $item->pdorder->payment_time;
                $item->payment_time = $payment_time ?: '未知';
                // 订单名称
                $info = $this->parseOrderInfo($item->product_type_id, $item->product_id, $item->section_id);

                $item->title     = $info['title'];
                $item->type_text = $info['type'];
            });
    }

    /**
     * 解析订单名称和类型
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-31
     *
     * @param int $product_type_id [description]
     * @param int $product_id      [description]
     * @return   [type]                                          [description]
     */
    public function parseOrderInfo(int $product_type_id, int $product_id, $section_id = 0)
    {
        switch ($product_type_id) {
            // 余额充值订单
            case ProductType::getProductIdByKey('balance'):
                $title = '余额充值';
                $type  = '余额充值';
                break;
            // 会员充值
            case ProductType::getProductIdByKey('vip'):
                $title = VipConfig::where('id', $product_id)->value('title');
                $title = $title ? '会员充值【' . $title . '】' : '会员充值';
                $type  = '会员充值';
                break;
            // 积分充值
            case ProductType::getProductIdByKey('credit'):
                $title = '积分充值';
                $type  = '积分充值';
                break;
            // 课程
            case 1:
            case 2:
            case 3:
            case 4:
                // 课程标题和类型
                $title = Course::withTrashed()->where('id', $product_id)->value('title');
                if ($section_id) {
                    // 查询课时名称
                    $title .= ' - ' . CourseSection::withTrashed()->where('id', $section_id)->value('title');
                    $title = '购买课时【' . $title . '】';
                } else {
                    $title = $title ? '购买课程【' . $title . '】' : '购买课程';
                }

                $type = ProductType::getProductTitleById($product_type_id);

                break;
            // 公开考试
            case ProductType::getProductIdByKey('exam'):
                $exam  = ModuleExamination::withTrashed()->with(['module', 'paper'])->where('id', $product_id)->find();
                $title = '【资源已被删除】';
                if ($exam) {
                    $title = $exam->module_title . ' - ' . $exam->title;
                }
                $type = ProductType::getProductTitleById($product_type_id);
                break;
            // 专项练习
            case ProductType::getProductIdByKey('special'):
                $exam  = ModuleSpecial::withTrashed()->with(['module'])->where('id', $product_id)->find();
                $title = '【资源已被删除】';
                if ($exam) {
                    $title = $exam->module_title . ' - ' . $exam->title;
                }
                $type = ProductType::getProductTitleById($product_type_id);
                break;
            // 套卷练习
            case ProductType::getProductIdByKey('rollup'):
                $exam  = ModuleRollup::withTrashed()->with(['module'])->where('id', $product_id)->find();
                $title = '【资源已被删除】';
                if ($exam) {
                    $title = $exam->module_title . ' - ' . $exam->title;
                }
                $type = ProductType::getProductTitleById($product_type_id);
                break;
            // 商品
            case ProductType::getProductIdByKey('goods'):
                $product = Product::withTrashed()->where('id', $product_id)->value('title');
                $title   = '【资源已被删除】';
                if ($product) {
                    $title = $product;
                }
                $type = ProductType::getProductTitleById($product_type_id);
                break;
            default:
                $ptitle = ProductType::getProductTitleById($product_type_id);
                $title  = $ptitle ? '购买【' . $ptitle . '】' : '未知';
                $type   = $ptitle ? $ptitle : '未知';
                break;
        }

        return ['title' => $title, 'type' => $type];
    }

}
